import {
  Body,
  Controller,
  Delete,
  Param,
  Post,
  Put,
  Request,
} from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";
import { JwtPayload } from "src/utils/interfaces/jwt.interface";
import { DeliverOrderService } from "./deliver-order.service";
import { CreateDeliveryDto, ItemDto } from "./dto/create-delivery.dto";
import { UpdateDeliveryItemDto } from "./dto/update-delivery-item.dto";

@Controller("deliver-order")
@ApiTags("送货")
@ApiBearerAuth()
export class DeliverOrderController {
  constructor(private readonly deliverOrderService: DeliverOrderService) {}

  // 为当前公司创建送货单
  @Post()
  create(
    @Request() req: { user: JwtPayload },
    @Body() createDeliveryDto: CreateDeliveryDto
  ) {
    const companyId = req.user.company;
    const userId = req.user.sub;
    return this.deliverOrderService.deliver(
      createDeliveryDto,
      companyId,
      userId
    );
  }

  // 删除某一个送货单项
  @Put("delete/:id/:deliveryItemId")
  delete(@Param("deliveryItemId") deliveryItemId: string) {
    return this.deliverOrderService.deliveryDeleteItem(deliveryItemId);
  }

  // 为指定送货单增加送货单项
  @Put("append/:id")
  append(@Param("id") id: string, @Body() itemDto: ItemDto) {
    return this.deliverOrderService.deliveryAppendItem(id, itemDto);
  }

  // 修改某一个送货单项
  @Put("update/:id")
  update(
    @Param("id") id: string,
    @Body() updateDeliveryDto: UpdateDeliveryItemDto
  ) {
    return this.deliverOrderService.deliveryUpdateItem(id, updateDeliveryDto);
  }

  // 删除送货单
  @Delete(":id")
  remove(@Param("id") id: string) {
    return this.deliverOrderService.unDeliver(id);
  }
}
